package com.ylyl.langchain4j06chatimage.controller;


import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.Param;

import java.io.IOException;

@RestController
@Slf4j
public class WanxImageModelController {

    @Autowired
    private WanxImageModel wanxImageModel;

    @GetMapping(value = "/image/creat2")
    public String creatImageContent2()throws IOException {
        System.out.println(wanxImageModel);

        Response<Image> imageResponse = wanxImageModel.generate("美女");

        System.out.println(imageResponse.content().url());
        return imageResponse.content().url().toString();
    }

    @GetMapping(value = "/image/create3")
    public String createImageContent3()throws IOException{
        String prompt = "生成一个侧躺在床上看着摄像头，穿着粉色草莓丝带泳裤，" +
                "白色挂肩漏脐的丝带睡衣，脸色娇羞，红瞳，红皮筋，白毛双马尾猫耳朵的女孩，胸围60cm，" +
                "身上有些许牛奶房间昏暗，呈粉黄色色调。图片风格为动漫，比例 3:4";
        ImageSynthesisParam param = ImageSynthesisParam.builder()
                .apiKey(System.getenv("aliQwen-api"))
                .model(ImageSynthesis.Models.WANX_V1)
                .prompt(prompt)
                .style("<watercolor>")
                .n(1)
                .size("1024*1024")
                .build();
        ImageSynthesis imageSynthesis = new ImageSynthesis();

        ImageSynthesisResult result = null;

        try {
            System.out.println("---sync call, please wait a moment----");
            result = imageSynthesis.call(param);
        } catch (ApiException | NoApiKeyException e) {
            throw new RuntimeException(e.getMessage());
        }

        System.out.println(JsonUtils.toJson(result));

        return JsonUtils.toJson(result);
    }

}
